package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.commlib.base.BaseLoadmoreAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.baoding.R
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.databinding.*
import client.comm.baoding.ui.TdActivity
import client.comm.baoding.ui.TdlstjActivity

import com.bumptech.glide.RequestManager

class TdAdapter : BaseLoadmoreAdapter<TdBean.Sub> {

    val manager: RequestManager
    lateinit var goodsIndex: TdBean

    constructor(context: Context, manager: RequestManager) : super(context) {
        this.manager = manager
    }

    override fun getItemViewType(position: Int): Int {
        if (position == 0) return 1
        return super.getItemViewType(position)
    }

    override fun createVh(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 1)
            return BindingViewHoder(
                DataBindingUtil.inflate(
                    layoutInflater,
                    R.layout.layout_tdheader,
                    parent,
                    false
                )
            )
        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.layout_tditem,
                parent,
                false
            )
        )
    }

    fun submitData(list: MutableList<TdBean.Sub?>, goodsIndex: TdBean) {
        this.goodsIndex = goodsIndex
        list.add(0, TdBean.Sub("", 1, "", "", "", "", "", "", "", "", "",""))
        super.submitList(list)
    }

    override fun bindVh(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        when (bindingViewHolder.itemViewType) {
            1 -> {
                val binding = bindingViewHolder.binding as LayoutTdheaderBinding
                binding.name = goodsIndex.nickname
                binding.subStr = "代理购买${goodsIndex.team_buy}"
            }
            else -> {
                val binding = bindingViewHolder.binding as LayoutTditemBinding
                val bean = getItem(position)
                manager.load(bean.avatar).error(R.mipmap.ic_launcher).into(binding.img)
                binding.bean = bean
                binding.event = this

//                if(bean!=null){
//                    if (position == 1) {
//                        binding.llHeader.visibility = View.VISIBLE
//                    } else {
//                        binding.llHeader.visibility = View.GONE
//                    }
//                }


            }
        }
    }

    fun onItemClick(v: View, b: TdBean.Sub) {
        context.startActivity(Intent(context, TdActivity::class.java).apply {
            putExtra("bean", b.id)
        })
    }

    /**
     * 历史推荐
     */
    fun lstj(v: View){
        context.startActivity(Intent(context, TdlstjActivity::class.java))
    }

}